**********************************************************************************************
* Replication file: "Low skill products by high skill workers. The distributive effects of trade in emerging and developing countries"
* Irene Menéndez González, Erica Owen and Stefanie Walter. December 2022
**********************************************************************************************

**********************************************************************************************
* Code to generate results in Table A17 using a 5-year panel, 1960-2016
**********************************************************************************************

clear
set more off

capture log close

use DataIneqTrade_ready.dta, clear


* DV: market and disposable income inequality
***********************************************************************
* mgini_mk: measure of market income inequality containing the row mean of the inequality estimates
 
* mgini_dp: measure of disposable income inequality containing the row mean of the inequality estimates



* Main IVs
*****************

* Trade volume: imports + exports as % GDP
gen tradef = trade/100

* Exports in manufacturing (% merchandise)
gen manufexpf = manufexp/100

* Imports in manufacturing (% merchandise)
gen manufimpf = manufimp/100

* Exports in agriculture (% of merchandise) 
gen agrexpf = agrexp/100

* Imports in agriculture (% of merchandise)
gen agrimpf = agrimp/100

* Exports in food (% of merchandise) 
gen foodexpf = foodexp/100

* Imports in food (% of merchandise)
gen foodimpf = foodimp/100

* Exports in fuels (% of merchandise) 
gen fuelexpf=fuelexp/100

* Imports in fuels (% of merchandise) 
gen fuelimpf=fuelimp/100

* Sum of commodities
gen comtotalx=agrexpf + foodexpf + fuelexpf
gen comtotalm=agrimpf + foodimpf + fuelimpf


* Polity indicator
gen demo=.
replace demo=1 if polity>=6
replace demo=0 if polity<6
lab def de 0 "Non-democracy" 1 "Democracy"
lab val demo de
lab var demo "Democracy"

* Ideology
gen ideo=.
replace ideo=1 if execrlc==3
replace ideo=0 if execrlc==2
replace ideo=0 if execrlc==1

lab def id 0 "Center/Right" 1 "Left"
lab val ideo id
lab var ideo "Ideology"

* Proportional representation - pr (3 categories) 
recode pr (1=2) (0=1) (-999=0)
lab def r 0 "NA" 1 "Plurality" 2 "PR"
lab val pr r
lab var pr "Proportional representation"

* Capita GDP (log) 
gen log_gdp=log(gdpcap)

* Capita GDP squared
gen gdpsq=log_gdp^2

* Population growth (log)
gen log_pop=log(pop)

* Growth gdp (log): annual % growth rate GDP
gen log_grthgdp=log(grthgdp)


xtset acode year 

* Lags

gen manufexpfL1=L1.manufexpf

gen manufimpfL1=L1.manufimpf

gen foodexpfL1=L1.foodexpf

gen foodimpfL1=L1.foodimpf

gen agrexpfL1=L1.agrexpf

gen agrimpfL1=L1.agrimpf

gen fuelexpfL1=L1.fuelexpf

gen fuelimpfL1=L1.fuelimpf

gen comtotalxL1=L1.comtotalx

gen comtotalmL1=L1.comtotalm

gen popL1=L1.pop

gen log_gdpL1=L1.log_gdp

gen gdpsqL1=L1.gdpsq

gen tradefL1=L1.tradef

gen grthgdpL1=L1.grthgdp

gen prL1=L1.pr

gen ideoL1=L1.ideo

gen demoL1=L1.demo

* Period dummies
gen period=.

replace period=0 if year==1960
replace period=0 if year==1961
replace period=0 if year==1962
replace period=0 if year==1963
replace period=0 if year==1964
replace period=0 if year==1965
replace period=0 if year==1966
replace period=0 if year==1967
replace period=0 if year==1968
replace period=0 if year==1969
replace period=1 if year==1970
replace period=1 if year==1971
replace period=1 if year==1972
replace period=1 if year==1973
replace period=1 if year==1974
replace period=1 if year==1975
replace period=1 if year==1976
replace period=1 if year==1977
replace period=1 if year==1978
replace period=1 if year==1979
replace period=2 if year==1980
replace period=2 if year==1981
replace period=2 if year==1982
replace period=2 if year==1983
replace period=2 if year==1984
replace period=2 if year==1985
replace period=2 if year==1986
replace period=2 if year==1987
replace period=2 if year==1988
replace period=2 if year==1989
replace period=3 if year==1990
replace period=3 if year==1991
replace period=3 if year==1992
replace period=3 if year==1993
replace period=3 if year==1994
replace period=3 if year==1995
replace period=3 if year==1996
replace period=3 if year==1997
replace period=3 if year==1998
replace period=3 if year==1999
replace period=4 if year==2000
replace period=4 if year==2001
replace period=4 if year==2002
replace period=4 if year==2003
replace period=4 if year==2004
replace period=4 if year==2005
replace period=4 if year==2006
replace period=4 if year==2007
replace period=4 if year==2008
replace period=4 if year==2009
replace period=5 if year==2010
replace period=5 if year==2011
replace period=5 if year==2012
replace period=5 if year==2013
replace period=5 if year==2014
replace period=5 if year==2015
replace period=5 if year==2016

xtset acode year 

gen sample5y=0
replace sample5y=1 if year== 1960 | year==1965 | year==1970 | year==1975 | year==1980 | year==1985 | year==1990 | year==1995 | year==2000 | year==2005 | year==2010 | year==2015

keep if sample5y==1

xtset acode year, delta(5) 

gen mgini_mkL1=L1.mgini_mk

gen mgini_dpL1=L1.mgini_dp

list country_solt year mgini_mkL1 mgini_mk if country_solt=="United States"

xtreg mgini_mk mgini_mkL1 manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using sample5y.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes, Period FE, Yes) 

xtreg mgini_dp mgini_dpL1 manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using sample5y.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes, Period FE, Yes)